package com.alipay.sdk.abcphaima;

import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.iot.bpaas.api.BPaaSApi;
import com.alipay.iot.bpaas.api.service.BPaaSCallback;
import com.alipay.iot.bpaas.api.service.BPaaSResponse;
import com.alipay.iot.bpaas.api.service.LocalService;
import com.alipay.sdk.util.LogUtil;
import com.alipay.sdk.util.ScanKeyUtil;

import lombok.val;

/**
 * @ProjectName: 团餐
 * @ClassName: AbcpHMFree
 * @Description: java类作用描述
 * @Author: bd-001935
 * @CreateDate: 2022/8/15 5:10 下午
 */
public class AbcpHMFree {

    private static String mSessionId;

    /**
     * 启动刷脸
     */
    public static void startFreeFace(String amount, int expireTime, AbcpHM.AbcpScanFaceCallback callback) {
        if (!AbcpHM.getInstance().isAbcpInitSuccess()) {
            return;
        }
        ScanKeyUtil scanKeyUtil = new ScanKeyUtil(value -> {
             callback.onFail("qrCode", value);
        });

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tradeAmount", amount);
        jsonObject.put("expireTime", expireTime);
        AbcpHM.startCanteenBPaaSService("faceVerify", "startVerify",
                jsonObject, new BPaaSCallback() {
                    @Override
                    public void onResponse(BPaaSResponse response) {
                        LogUtil.d("启动刷脸 出参" + response);
                        try {
                            if (response == null || response.getCode() == BPaaSResponse.RESULT_CODE_FAIL) {
                                // bpaas服务调用失败
                                callback.onFail("", "bpaas服务调用失败");
                            } else if (response.getResult() != null) {

                                if (response.getCode() == BPaaSResponse.RESULT_CODE_SUCCESS) {
                                    Bundle result = response.getResult().getBundle(LocalService.KEY_LOCAL_RESULT);

                                    if (result != null) {
                                        String codes = String.valueOf(result.getInt("code"));
                                        // 处理刷脸结果
                                        String subCode = result.getString("subCode");
                                        String subMessage = result.getString("subMessage");
                                        String sessionId = result.getString("sessionId");
                                        mSessionId = result.getString("sessionId");
                                        String tradeNo = result.getString("tradeNo");
                                        String barCode = result.getString("barCode");
                                        String authCode, alipayUid = "";
                                        LogUtil.d("启动刷脸 出参" + result);
                                        if (subCode.equals("SCAN_BAR_CODE")) {
                                            authCode = result.getString("scanCode");
                                        } else {
                                            alipayUid = result.getString("alipayUid");
                                            authCode = result.getString("ftoken");
                                        }
                                        LogUtil.d("刷脸结果"
                                                + "\nsessionId = " + sessionId
                                                + "\ntradeNo = " + tradeNo
                                                + "\nauthCode = " + authCode
                                                + "\nbarCode = " + barCode
                                                + "\nalipayUid = " + alipayUid);
                                        if (TextUtils.isEmpty(authCode) || TextUtils.isEmpty(tradeNo) || TextUtils.isEmpty(sessionId)) {
                                            callback.onFail(subCode, subMessage);
                                        } else {
                                            callback.onSuccess(!subCode.equals("SCAN_BAR_CODE"),
                                                    authCode, barCode, tradeNo, sessionId, alipayUid);
                                        }
                                    }
                                } else {
                                    String subCode = response.getSubCode();
                                    String subMessage = response.getSubMsg();
                                    LogUtil.d("启动刷脸 出参"
                                            + "\nsubCode = " + subCode
                                            + "\nsubMessage = " + subMessage);
                                    callback.onFail(subCode, subMessage);
                                }
                            }
                        } catch (Exception e) {
//                            Z1008
                            callback.onFail("Z1008", "subMessage");
                        }

                    }

                    @Override
                    public void onEvent(String eventId, String data, Bundle extra) {
                        System.out.println("按键按下");
                        LogUtil.d("启动刷脸 onEvent"
                                + "\neventId = " + eventId
                                + "\ndata = " + data
                                + "\nextra = " + extra);
                        JSONObject response = JSON.parseObject(data);
                        String eventType = response.getString("messageType");
                        JSONObject content = JSON.parseObject(response.getString("messageContent"));
                        if (eventType.equals("SMILE_NEW_SESSION")) {
                            // 每次刷脸都会将当次的sessionId和tradeNo通过onEvent回调过来
                            String sessionId = content.getString("sessionId");
                            String tradeNo = content.getString("tradeNo");
                            LogUtil.d("启动刷脸 onEvent"
                                    + "\nsessionId = " + sessionId
                                    + "\ntradeNo = " + tradeNo
                                    + "\ncontent = " + content);
                            callback.onEvent(sessionId);
                        } else if ("SMILE_KEY_EVENT".equals(eventType)) {
                            scanKeyUtil.input(content.getJSONObject("result").getString("keyCode"));
                        }
                    }
                });
    }

    /**
     * 退出刷脸
     *
     * @param sessionId
     */
    public static void closeFreeFace(String sessionId) {
        if (!AbcpHM.getInstance().isAbcpInitSuccess()) {
            return;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sessionId", sessionId);

        AbcpHM.startCanteenBPaaSService("faceVerify", "close",
                jsonObject, new BPaaSCallback() {
                    @Override
                    public void onResponse(BPaaSResponse response) {
                        LogUtil.d("退出刷脸 出参" + response);
                    }

                    @Override
                    public void onEvent(String eventId, String data, Bundle extra) {
                        LogUtil.d("退出刷脸 onEvent"
                                + "\neventId = " + eventId
                                + "\ndata = " + data
                                + "\nextra = " + extra);
                    }
                });
    }

}
